
<template>
  <div class="asset-categories">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>资产建档</span>
          <el-button type="primary" @click="handleAdd">新建资产档案</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="资产分类">
          <el-select v-model="searchForm.category" placeholder="请选择资产分类" clearable>
            <el-option label="固定资产" value="fixed" />
            <el-option label="低值易耗品" value="low_value" />
            <el-option label="租赁资产" value="lease" />
            <el-option label="租入固定资产改良" value="lease_improvement" />
          </el-select>
        </el-form-item>
        <el-form-item label="资产名称">
          <el-input v-model="searchForm.name" placeholder="请输入资产名称" clearable />
        </el-form-item>
        <el-form-item label="使用部门">
          <el-select v-model="searchForm.department" placeholder="请选择使用部门" clearable>
            <el-option label="财务部" value="finance" />
            <el-option label="行政部" value="admin" />
            <el-option label="技术部" value="tech" />
            <el-option label="市场部" value="marketing" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="code" label="资产编码" width="120" />
        <el-table-column prop="name" label="资产名称" width="180" />
        <el-table-column prop="category" label="资产分类" width="120">
          <template #default="scope">
            <el-tag :type="getCategoryType(scope.row.category)">
              {{ getCategoryLabel(scope.row.category) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="model" label="规格型号" width="150" />
        <el-table-column prop="department" label="使用部门" width="120" />
        <el-table-column prop="user" label="使用人" width="120" />
        <el-table-column prop="location" label="存放位置" width="150" />
        <el-table-column prop="status" label="资产状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusLabel(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="purchaseDate" label="购置日期" width="120" />
        <el-table-column prop="value" label="资产价值" width="120">
          <template #default="scope">
            ¥{{ scope.row.value.toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="handlePrint(scope.row)">打印标签</el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

// 搜索表单
const searchForm = reactive({
  category: '',
  name: '',
  department: ''
})

// 表格数据
const tableData = ref([
  {
    id: 1,
    code: 'ZC20230001',
    name: '笔记本电脑',
    category: 'fixed',
    model: 'ThinkPad X1 Carbon',
    department: '技术部',
    user: '张三',
    location: '总部A座501',
    status: 'in_use',
    purchaseDate: '2023-01-15',
    value: 8500,
    images: ['laptop1.jpg', 'laptop2.jpg'],
    description: '技术部开发用笔记本电脑，配置较高'
  },
  {
    id: 2,
    code: 'ZC20230002',
    name: '办公桌',
    category: 'low_value',
    model: '标准办公桌',
    department: '行政部',
    user: '李四',
    location: '总部B座201',
    status: 'in_use',
    purchaseDate: '2023-02-20',
    value: 1200,
    images: ['desk1.jpg'],
    description: '行政部办公用桌'
  },
  {
    id: 3,
    code: 'ZC20230003',
    name: '投影仪',
    category: 'lease',
    model: '爱普生CH-TW5700',
    department: '市场部',
    user: '王五',
    location: '总部C座301',
    status: 'in_use',
    purchaseDate: '2023-03-10',
    value: 4500,
    images: ['projector1.jpg'],
    description: '市场部会议用投影仪，租赁资产'
  }
])

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 100
})

// 获取资产分类标签
const getCategoryLabel = (category) => {
  const categoryMap = {
    fixed: '固定资产',
    low_value: '低值易耗品',
    lease: '租赁资产',
    lease_improvement: '租入固定资产改良'
  }
  return categoryMap[category] || category
}

// 获取资产分类类型
const getCategoryType = (category) => {
  const typeMap = {
    fixed: '',
    low_value: 'success',
    lease: 'warning',
    lease_improvement: 'info'
  }
  return typeMap[category] || ''
}

// 获取资产状态标签
const getStatusLabel = (status) => {
  const statusMap = {
    in_use: '在用',
    idle: '闲置',
    maintenance: '维修中',
    scrapped: '已报废'
  }
  return statusMap[status] || status
}

// 获取资产状态类型
const getStatusType = (status) => {
  const typeMap = {
    in_use: 'success',
    idle: 'info',
    maintenance: 'warning',
    scrapped: 'danger'
  }
  return typeMap[status] || ''
}

// 搜索
const handleSearch = () => {
  console.log('搜索条件：', searchForm)
  ElMessage.success('搜索成功')
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  ElMessage.success('重置成功')
}

// 新建资产档案
const handleAdd = () => {
  router.push('/asset-categories/add')
}

// 查看资产详情
const handleView = (row) => {
  router.push(`/assets/detail/${row.id}`)
}

// 编辑资产
const handleEdit = (row) => {
  router.push(`/asset-categories/edit/${row.id}`)
}

// 打印资产标签
const handlePrint = (row) => {
  ElMessage.success(`正在打印资产 ${row.name} 的标签...`)
  // 这里实际应该调用打印功能
}

// 删除资产
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除资产 "${row.name}" 吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 实际应该调用API删除
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.pageSize = size
  // 实际应该重新加载数据
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.currentPage = page
  // 实际应该重新加载数据
}
</script>

<style scoped>
.asset-categories {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
